package mobi.pooper;


import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Button;
import com.badlogic.gdx.scenes.scene2d.ui.Button.ButtonStyle;
import com.badlogic.gdx.scenes.scene2d.ui.CheckBox;
import com.badlogic.gdx.scenes.scene2d.ui.CheckBox.CheckBoxStyle;
import com.badlogic.gdx.scenes.scene2d.ui.ClickListener;
import com.badlogic.gdx.scenes.scene2d.ui.FlickScrollPane;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Label.LabelStyle;
import com.badlogic.gdx.scenes.scene2d.ui.List;
import com.badlogic.gdx.scenes.scene2d.ui.List.ListStyle;
import com.badlogic.gdx.scenes.scene2d.ui.ScrollPane;
import com.badlogic.gdx.scenes.scene2d.ui.ScrollPane.ScrollPaneStyle;
import com.badlogic.gdx.scenes.scene2d.ui.SelectBox;
import com.badlogic.gdx.scenes.scene2d.ui.SelectBox.SelectBoxStyle;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Slider;
import com.badlogic.gdx.scenes.scene2d.ui.Slider.SliderStyle;
import com.badlogic.gdx.scenes.scene2d.ui.Slider.ValueChangedListener;
import com.badlogic.gdx.scenes.scene2d.ui.SplitPane;
import com.badlogic.gdx.scenes.scene2d.ui.SplitPane.SplitPaneStyle;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton.TextButtonStyle;
import com.badlogic.gdx.scenes.scene2d.ui.TextField;
import com.badlogic.gdx.scenes.scene2d.ui.TextField.TextFieldListener;
import com.badlogic.gdx.scenes.scene2d.ui.TextField.TextFieldStyle;
import com.badlogic.gdx.scenes.scene2d.ui.Window;
import com.badlogic.gdx.scenes.scene2d.ui.Window.WindowStyle;
import com.badlogic.gdx.scenes.scene2d.ui.tablelayout.Table;

public class MainMenuScreen implements Screen {

	protected static final String TAG = MainMenuScreen.class.getSimpleName();

	private static final String BTN_GAME = "btnGame";
	private static final String BTN_BROWSE = "btnBrowse";
	private static final String BTN_STAT = "btnStat";

	private Pooper pooper;

	Skin skin;
	Stage stage;
	SpriteBatch batch;
	Actor root;

	private ClickListener btnListener = new ClickListener(){

		@Override
		public void click(Actor actor, float x, float y) {

			Gdx.app.debug(TAG, "actor = " + actor.name + " x = " + x + " y = " + y);
			
			MainMenuScreen.this.dispose();

			if(BTN_GAME.equals(actor.name)){
				pooper.startGame();
			}else if(BTN_BROWSE.equals(actor.name)){
				pooper.startPicturesBrowser();
			}else if(BTN_STAT.equals(actor.name)){
				Gdx.app.debug(TAG, "BLEAT");
				pooper.startShowingStat();
			}
		}
	};

	public MainMenuScreen(Pooper pooper) {
		this.pooper = pooper;
		batch = new SpriteBatch();
		skin = new Skin(Gdx.files.internal("data/ui/uiskin.json"), Gdx.files.internal("data/ui/uiskin.png"));
		stage = new Stage(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), false);
		Gdx.input.setInputProcessor(stage);

		final Button btnGame = new TextButton("GAME", skin.getStyle(TextButtonStyle.class), BTN_GAME);
		btnGame.setClickListener(btnListener);
		final Button btnBrowse = new TextButton("BROWSE PICTURES", skin.getStyle(TextButtonStyle.class), BTN_BROWSE);
		btnBrowse.setClickListener(btnListener);
		final Button btnStat = new TextButton("SHOW STATISTICS", skin.getStyle(TextButtonStyle.class), BTN_STAT);
		btnStat.setClickListener(btnListener);
		final Label fpsLabel = new Label("fps:", skin.getStyle(LabelStyle.class), "label");

		Table table = new Table(skin);
		table.x = table.y = 100;

		table.add(btnGame);
		table.row();
		table.add(btnBrowse);
		table.row();
		table.add(btnStat);
		table.row();
		table.add(fpsLabel);

		stage.addActor(table);
	}	

	@Override
	public void dispose() {
		stage.dispose();
		skin.dispose();
	}

	@Override
	public void hide() {
		// TODO Auto-generated method stub

	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub

	}

	@Override
	public void render(float arg0) {
		Gdx.gl.glClearColor(0.2f, 0.2f, 0.2f, 1);
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

		((Label)stage.findActor("label")).setText("fps: " + Gdx.graphics.getFramesPerSecond());

		stage.act(Math.min(Gdx.graphics.getDeltaTime(), 1 / 30f));
		stage.draw();
	}

	@Override
	public void resize(int width, int height) {
		stage.setViewport(width, height, false);
	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub

	}

	@Override
	public void show() {
		// TODO Auto-generated method stub

	}

}
